import {recommendSong} from '../../api/song'
import {get} from 'lodash'

const state = {
  recommendMusic: [],
  test: 'hello test',
  inputModel: 'hello'
}

const getters = {
  myTestGetters: state => state.test,
  myInputModel: state => state.inputModel,
  getTestLength: (state, getters, rootState,rootGetters) => {
    return getters.myTestGetters.length
  },
  getMyTest: state => (condition) => {
    if (condition) {
      return state.test
    } else return '啥都没有'
  },
  getTopRecommendMusic: state => (count) => {
    if (state.recommendMusic.length < count) {
      return state.recommendMusic
    } else {
      let offset = Math.random() * (state.recommendMusic.length - count)
      return state.recommendMusic.slice(offset, count + offset)
    }
  }
}

const mutations = {
  addString(state) {
    state.test += 'add success'
  },
  addMyString(state, {text}) {
    state.test += text
  },
  addMyObj(state, {name}) {
    state.test += name
  },
  addRecommendMusic(state, payload) {
    if (payload) {
      state.recommendMusic = payload
    }
  },
  addInputModel(state, payload) {
    state.inputModel = payload
  }
}

const actions = {
  addStringActions({commit, rootState}) {
    // console.log(get(rootState, 'request'))
    // setTimeout(() => {
    //   console.log('log in')
    //   commit('addString')
    // }, 5000)
  },
  storeGetRecommendSong({commit}) {
    recommendSong().then((data) => {
      commit('addRecommendMusic', get(data, 'data.data'))
    })
  }
}


export default {
  state,
  getters,
  mutations,
  actions
}
